package com.edu.app.controller.linkcontrollers;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.edu.app.bean.core.SysCustomer;
import com.edu.app.bean.core.SysUser;
import com.edu.app.bean.linkbeans.SysUserCustomer;
import com.edu.app.constant.StaticConstant;
import com.edu.app.constant.SysMessage;
import com.edu.app.service.sys.core.SysCustomerService;
import com.edu.app.service.sys.linkservices.SysUserCustomerService;
import com.edu.app.util.CommonUtils;
import com.edu.app.util.ErrorUtil;
import com.edu.app.util.JWTUtil;
import com.edu.app.util.MsgUtil;

import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RequestMapping("/api/user_customer")
@RestController
public class SysUserCustomerController {
    @Autowired
    private SysUserCustomerService sysUserCustomerService;

    @Autowired
    private SysCustomerService sysCustomerService;

    //使用get需要添加默认get函数
    @GetMapping(value = "")
    @RequiresAuthentication
    public ModelMap getUserCustomerList(){
        List<SysUserCustomer> sysUserCustomerList = sysUserCustomerService.getObjectList();
        return MsgUtil.retMsg(SysMessage.GET_USER_CUSTOMER_SUCCEED, StaticConstant.USER_CUSTOMER_LIST, sysUserCustomerList);
    }

    @GetMapping(value = "/getCustomerByUserId")
    @RequiresAuthentication
    public ModelMap getCustomerByUserId(@RequestParam Integer id){
        List<SysCustomer> sysCustomerList = sysUserCustomerService.getCustomerByUserId(id);
        return MsgUtil.retMsg(SysMessage.GET_CUSTOMER_SUCCEED, StaticConstant.CUSTOMER_LIST, sysCustomerList);
    }

    @GetMapping(value = "/getUserByCustomerId")
    @RequiresAuthentication
    public ModelMap getUserByCustomerId(@RequestParam Integer id){
        List<SysUser> sysUserList = sysUserCustomerService.getUserByCustomerId(id);
        return MsgUtil.retMsg(SysMessage.GET_USER_SUCCEED, StaticConstant.USER_LIST, sysUserList);
    }

    @GetMapping(value = "/getUserByCustomerIdWithRole")
    @RequiresAuthentication
    public ModelMap getUserByCustomerIdWithRole(@RequestParam Integer id, @RequestParam String role_name){
        List<SysUser> sysUserList = sysUserCustomerService.getUserByCustomerIdWithRole(id, role_name);
        return MsgUtil.retMsg(SysMessage.GET_USER_SUCCEED, StaticConstant.USER_LIST, sysUserList);
    }

    @DeleteMapping(value = "/deleteUserCustomerByCustomerId")
    @RequiresAuthentication
    public ModelMap deleteUserCustomerByCustomerId(@RequestParam Integer id){
        try{
            sysUserCustomerService.deleteUserCustomerByCustomerId(id);
            return MsgUtil.retMsg(SysMessage.DELETE_USER_CUSTOMER_SUCCEED, "id", id);
        }catch(Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

    @DeleteMapping(value = "/deleteUserCustomerByUserId")
    @RequiresAuthentication
    public ModelMap deleteUserCustomerByUserId(@RequestParam Integer id){
        try{
            sysUserCustomerService.deleteUserCustomerByUserId(id);
            return MsgUtil.retMsg(SysMessage.DELETE_USER_CUSTOMER_SUCCEED, "id", id);
        }catch(Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

    @GetMapping(value = "/pages/getMyCustomer")
    @RequiresAuthentication
    public ModelMap getMyCustomer(@RequestHeader("Authorization") String token, @RequestParam Integer offset, @RequestParam Integer limit){
        try{
            String username = JWTUtil.getUsername(token);
            PageInfo pageInfo = sysUserCustomerService.getUserCustomerByPage(username, offset, limit);
            return MsgUtil.retMsg(SysMessage.GET_USER_CUSTOMER_SUCCEED, "page_info", pageInfo);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

    @GetMapping(value = "/pages/getMyPoCustomer")
    @RequiresAuthentication
    public ModelMap getMyPoCustomer(@RequestHeader("Authorization") String token, @RequestParam Integer offset, @RequestParam Integer limit){
        try{
            String username = JWTUtil.getUsername(token);
            PageInfo pageInfo = sysUserCustomerService.getUserPoCustomerByPage(username, offset, limit);
            return MsgUtil.retMsg(SysMessage.GET_USER_ACTIVITY_SUCCEED, "page_info", pageInfo);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

    @PostMapping(value = "/assignCustomers")
    @RequiresAuthentication
    public ModelMap assignCustomers(@RequestHeader("Authorization") String token, @RequestBody String strIds, @RequestParam Integer userId, @RequestParam String rolePlay){
        try{
            String username = JWTUtil.getUsername(token);
            Date date = new Date();
            String data = "";
            JSONObject obj = (JSONObject) JSON.parse(strIds);
            List<Integer> idsArray = JSON.parseArray(obj.get("strIds").toString(), Integer.class);
            for(Integer customerId: idsArray){
                data += "(\"" + username + "\",\"" + CommonUtils.dateToString(date) + "\",\"" + username + "\",\"" + CommonUtils.dateToString(date) + "\"," + userId + "," + customerId + ",\"" + rolePlay + "\")";
                data += ",";
            }
            data = data.substring(0, data.length() - 1);
            sysUserCustomerService.assignCustomers(data);
            return MsgUtil.retMsg(SysMessage.ASSIGN_USER_CUSTOMER_SUCCEED, null, null);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

    @PostMapping(value = "/assignCustomersRandom")
    @RequiresAuthentication
    public ModelMap assignCustomersRandom(@RequestHeader("Authorization") String token, @RequestParam Integer userId, @RequestParam String rolePlay, @RequestParam Integer number, @RequestParam String channel_tag){
        try{
            String username = JWTUtil.getUsername(token);
            Date date = new Date();
            String data = "";
            List<SysCustomer> sysCustomerList = sysCustomerService.getCustomerByChannelTag(channel_tag, number);
            for(SysCustomer customer: sysCustomerList){
                data += "(\"" + username + "\",\"" + CommonUtils.dateToString(date) + "\",\"" + username + "\",\"" + CommonUtils.dateToString(date) + "\"," + userId + "," + customer.getId() + ",\"" + rolePlay + "\")";
                data += ",";
            }
            data = data.substring(0, data.length() - 1);
            sysUserCustomerService.assignCustomers(data);
            return MsgUtil.retMsg(SysMessage.ASSIGN_USER_CUSTOMER_SUCCEED, null, null);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }
}
